/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;

import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.modelo.Privilegio;
import muhnes.sizoo.HibernateUtilSeguridad;

public class PrivilegioDaoImpl implements PrivilegioDao {
    
    Session sesion;
    
    public PrivilegioDaoImpl() {
        sesion = HibernateUtilSeguridad.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }

    @Override
    public void guardar( Privilegio privilegio ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.save( privilegio );
        transaccion.commit();
    }

    @Override
    public void guardarColeccion( List< Privilegio > privilegios ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( privilegios );
        transaccion.commit();
    }

    @Override
    public void actualizar( Privilegio privilegio ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( privilegio );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Privilegio > privilegios ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( privilegios );
        transaccion.commit();
    }

    @Override
    public void eliminar( Privilegio privilegio ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( privilegio );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Privilegio > privilegios ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( privilegios );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Privilegio privilegio = buscarPorId( id );
        sesion.delete( privilegio );
        transaccion.commit();
    }

    @Override
    public Privilegio buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Privilegio where codigoPrivilegio = " + id);
        Privilegio privilegio = (Privilegio)consulta.uniqueResult();
        transaccion.commit();
        return privilegio;
    }

    @Override
    public List< Privilegio > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Privilegio");
        List< Privilegio > privilegios = consulta.list();
        transaccion.commit();
        return privilegios;
    }

}
